const fs = require("fs");
const path = require("path");
async function main() {
    console.log("开始部署网页");
    await import("node-fetch").then(({ default: fetch }) => {
        global.fetch = fetch;
    });
    const cookie = fs
        .readFileSync(path.resolve(__dirname, "./key.txt"), {
            encoding: "utf-8",
        })
        .trim();
    const csrfToken = await fetch(
        "https://gitee.com/dongzhongzhidong/code-snippets/pages",
        {
            headers: {
                "upgrade-insecure-requests": "1",
                cookie,
                Referer: "https://gitee.com/dongzhongzhidong/code-snippets",
            },
            method: "GET",
        }
    )
        .then((res) => res.text())
        .then((res) =>
            res.replace(
                /[\s\S]*?csrf\-token[\s\S]*?content="([\s\S]*?)"[\s\S]*/,
                "$1"
            )
        );
    console.log(csrfToken);
    const result = await fetch(
        "https://gitee.com/dongzhongzhidong/code-snippets/pages/rebuild",
        {
            headers: {
                "content-type":
                    "application/x-www-form-urlencoded; charset=UTF-8",

                "x-csrf-token": csrfToken,
                "x-requested-with": "XMLHttpRequest",
                cookie,
                Referer:
                    "https://gitee.com/dongzhongzhidong/code-snippets/pages",
            },
            body: "branch=release&build_directory=dist&force_https=false&auto_update=false",
            method: "POST",
        }
    ).then((res) => res.text());
    console.log(result);
    console.log("部署结束");
}
console.log("3 秒后准备部署");
setTimeout(() => {
    main();
}, 3000);
